package com.study.springboottest.leetcode;

public class Test2 {

    public static void main(String[] args) {

        //关于数组实现顺序表的操作例子有哪些
        Test2 test2 = new Test2(10);

    }

    //定义顺序表结构
    private int[] data;

    private int size;

    public Test2(int capacity) {
        data = new int[capacity];
        size = 0;
    }

    // 插入元素
    public boolean insert(int index, int value) {
        if (index < 0 || index > size) {
            return false;
        }
        if (size > data.length) {
            return false;
        }
        for (int i = size; i > index; i--) {
            data[i] = data[i - 1];
        }
        data[index] = value;
        size++;
        return true;
    }

    // 删除元素
    public boolean remove(int index) {
        if (index < 0 || index >= size) {
            return false;
        }
        for (int i = index; i < size - 1; i++) {
            data[i] = data[i + 1];
        }
        size--;
        return true;
    }

    // 按元素查找位置
    public int find(int value) {
        for (int i = 0; i < size; i++) {
            if (data[i] == value) {
                return i;
            }
        }
        return -1;
    }

    //按位置查找元素
    public int get(int index) {
        if (index < 0 || index >= size) {
            return -1;
        }
        return data[index];
    }
}
